/*
EP3D is a real-time 3D planet engine , which in addition to providing 
substandard scene rendering and scene management, of course, it also 
provides some basic class libraries to build the entire virtual 
planet, or even the entire universe.

Copyright (C) 2010  Hongjiang Zhang	(zhjwyat@gmail.com)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "EP3DCameraSceneNode.h"
namespace EP3D
{
	ImplementRtti(CameraSceneNode, SceneNode);

	CameraSceneNode::CameraSceneNode(SceneNode* pkParent, SceneManager* pkMgr, const Transformf& t, const Vector3f& at)
		:SceneNode(pkParent, pkMgr, t)
		,m_pkMgr(pkMgr)
		,m_kTarget(at)
		,m_kUpVector(0.0f, 1.0f, 0.0f)
		,m_fZNear(1.0f)
		,m_fZFar(10000.0f)
	{
		m_fFov = PI / 2.5;
		m_fAspect = 16.0f / 9.0f;

		CalculateProjectionMatrix();
		CalculateViewArea();
	}

	const Matrix4f& CameraSceneNode::GetViewMatrix() const
	{
		return m_kFrustum.GetViewMatrix();
	}

	void CameraSceneNode::SetProjectionMatrix(const Matrix4f& proj)
	{
		m_kFrustum.GetProjectionMatrix() = proj;
	}

	const Matrix4f& CameraSceneNode::GetProjectionMatrix() const
	{
		return m_kFrustum.GetProjectionMatrix();
	}

	void CameraSceneNode::SetTarget(const Vector3f& pos)
	{	
		m_kTarget = pos;
		Vector3f toTarget = m_kTarget - GetAbsolutePosition();
		toTarget.Normalize();

		Vector3f viewAxis = GetAbsoluteTransform().m_qRotate.GetViewAxis();
		Quaternionf q;
		q.FromRotationAxis(viewAxis, toTarget);
		m_kRelativeTransform.Rotate(q);
		
		if (m_pkParent) {
			m_kRelativeTransform.Rotate(m_pkParent->GetAbsoluteTransform().m_qRotate.Inverse());
		}
	}

	void CameraSceneNode::SetRotation(const Quaternionf& q)
	{
		SetRotation(q);
	}

	void CameraSceneNode::SetUpVector(const Vector3f& v)
	{
		m_kUpVector = v;
	}

	const Vector3f& CameraSceneNode::GetTarget() const
	{
		return m_kTarget;
	}

	const Vector3f& CameraSceneNode::GetUpVector() const
	{
		return m_kUpVector;
	}

	f32 CameraSceneNode::GetNearValue() const
	{
		return m_fZNear;
	}

	f32 CameraSceneNode::GetFarValue() const
	{
		return m_fZFar;
	}

	f32 CameraSceneNode::GetAspectRatio() const
	{
		return m_fAspect;
	}

	f32 CameraSceneNode::GetFov() const
	{
		return m_fFov;
	}

	void CameraSceneNode::SetNearValue(f32 f)
	{
		m_fZNear = f;
		CalculateProjectionMatrix();
	}

	void CameraSceneNode::SetFarValue(f32 f)
	{
		m_fZFar = f;
		CalculateProjectionMatrix();
	}

	void CameraSceneNode::SetAspectRatio(f32 f)
	{
		m_fAspect = f;
		CalculateProjectionMatrix();
	}

	void CameraSceneNode::SetFov(f32 f)
	{
		m_fFov = f;
		CalculateProjectionMatrix();
	}

	void CameraSceneNode::Render()
	{
		Vector3f pos = GetAbsolutePosition();
		Vector3f v = m_kTarget - pos;
		v.Normalize();

		Vector3f up = m_kUpVector;
		up.Normalize();

		f32 dpt = v.DotProduct(up);
		if (Mathf::Abs(dpt) > (1.0f - DELTA) && Mathf::Abs(dpt) < (1.0f + DELTA) ) {
			up.x += 0.5;
		}

		m_kFrustum.GetViewMatrix().LookAtLH(pos, m_kTarget, up);
		CalculateViewArea();

		//
	}

	const BBox3f& CameraSceneNode::GetBoundingBox() const
	{
		return m_kFrustum.GetBoundingBox();
	}

	const Frustum* CameraSceneNode::GetFrustum() const
	{
		return &m_kFrustum;
	}

	void CameraSceneNode::CalculateProjectionMatrix()
	{
		m_kFrustum.GetProjectionMatrix().PerspectiveFovLH(m_fFov, m_fAspect, m_fZNear, m_fZFar);
	}

	void CameraSceneNode::CalculateViewArea()
	{
		m_kFrustum.m_kPosition = GetAbsolutePosition();
		Matrix4f m = m_kFrustum.GetViewMatrix() * m_kFrustum.GetProjectionMatrix();
		m_kFrustum.SetFrom(m);
	}
}
